package com.k.kpp.web.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.k.kc.common.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author kivil
 */
@Controller
public class EasyExcelController {
    @Autowired
    private UploadDAO uploadDAO;

    @PostMapping("/easyExcel/upload")
    @ResponseBody
    public String read(HttpServletRequest req){
        MultipartHttpServletRequest mReq = (MultipartHttpServletRequest)req;
        MultipartFile file = mReq.getFile("file0");
        try {
            //EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDAO)).sheet().doRead();

            String filepath = "E:\\test\\ps1.xlsx";
            List<UploadData> list = EasyExcelFactory.read(filepath, UploadData.class,new UploadDataListener(uploadDAO)).sheet(1).doReadSync();
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
}
